Page({
  data:{
    loveText:"",
    show: false,
    loveValue:null,
    userOpenid:null,
    userName:null,
    userHead:null,
    loveArray:[],
    grant:false,
    checked:false
  },
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ checked: detail });
  },
   async getloveValue(e){
     this.setData({
      loveValue:e.detail
     })
  },
  // 提交表白内容
  async confirmSay(e){
    const ui= wx.getStorageSync('userInfo');
    if(!ui){
      wx.switchTab({
        url: '../mine/mine',
        success:res=>{
          wx.showToast({
            title: '请先登录',
            icon:'loading',
            duration:500
          })
        }
      })
    }else{
      this.setData({
        userHead:ui.avatarUrl,
        userName:ui.nickName,
        userOpenid:ui.openid
      })
      if(this.data.checked==true && this.data.loveValue && this.data.userOpenid){
        await wx.cloud.callFunction({
          name:"sayDear",
          data:{
            userHead:"cloud://andbridge.616e-andbridge-1301378677/hideHead/loveActive.png",
            userName:"匿名用户",
            userOpenid:this.data.userOpenid,
            loveValue:this.data.loveValue
          }
        }).then(async res=>{
          if(res.result){
          await this.getDear();
          this.onClose();
          wx.showToast({
            title: '成功',
            icon:'success'
          })
          this.setData({
            loveText:"",
            loveValue:null
          })
          }else{
            wx.showToast({
              title: '您的言论含有违规内容',
              icon:'none'
            })
          }
        }).catch(err=>{
          console.log(err)
          wx.showToast({
            title: '失败',
            icon:"none"
          })
        })
      }else if(this.data.checked==false && this.data.userOpenid && this.data.userHead && this.data.userName && this.data.loveValue){
          await wx.cloud.callFunction({
            name:"sayDear",
            data:{
              userHead:this.data.userHead,
              userName:this.data.userName,
              userOpenid:this.data.userOpenid,
              loveValue:this.data.loveValue
            }
          }).then(async res=>{
            if(res.result){
            await this.getDear();
            this.onClose();
            wx.showToast({
              title: '成功',
              icon:'success'
            })
            this.setData({
              loveText:"",
              loveValue:null
            })
            }else{
              wx.showToast({
                title: '您的言论含有违规内容',
                icon:'none'
              })
            }
          }).catch(err=>{
            console.log(err)
            wx.showToast({
              title: '失败',
              icon:"none"
            })
          })   
      }else{
    this.onClose();
    wx.showToast({
      title: '失败',
      icon:"none"
    })
  }  
}
  },
  // 获取表白内容
  async getDear(){
    await wx.cloud.callFunction({
      name:"getDear"
    }).then(async res=>{
      this.setData({
        loveArray:res.result.data
      })
    }).catch(err=>{
      console.log(err)
      wx.showToast({
        title: '获取失败，请刷新重试',
        icon:"none"
      })
    })
  },
  showPopup() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  async onPullDownRefresh(){
    await this.getDear();
    wx.stopPullDownRefresh();
  },
  async onShow(){
   await this.getDear();
  },
  async onLoad(){
    await this.getGrant();
  },
  async getGrant(){
    await wx.cloud.callFunction({
       name:"getGrant"
     }).then(res=>{
       this.setData({
         grant:res.result.data[0].switch
       })
     })
   }
})